package com.kwedding.util;

import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Component;

import springfox.documentation.spring.web.json.Json;

@Component
public class CheckedUtil extends LoggerParent{

	/**
	 * 是false抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isFalse(boolean expression, int errorCode, String errorMsg) {
		if (!expression)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 是true抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isTrue(boolean expression, int errorCode, String errorMsg) {
		if (expression)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 是false抛出异常 是null 抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isFalseObject(Boolean expression, int errorCode, String errorMsg) {
		if (expression == null || !expression)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 是true抛出异常 是null 抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isTrueObject(Boolean expression, int errorCode, String errorMsg) {
		if (expression == null || expression)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 是null抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNull(Object expression, int errorCode, String errorMsg) {
		if (expression == null)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 是null抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNotNull(Object expression, int errorCode, String errorMsg) {
		if (expression != null)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * list是null或者小于0抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNullAndSize(List<?> expression, int errorCode, String errorMsg) {
		if (expression == null || expression.size() < 1)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 数组是null或者小于0抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNullAndSize(Object[] expression, int errorCode, String errorMsg) {
		if (expression == null || expression.length < 1)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 数组是null或者小于0抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNullAndSize(Map<?, ?> map, int errorCode, String errorMsg) {
		if (map == null || map.size() < 1)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 字符串是null或者是空抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNullAndEmpty(String expression, int errorCode, String errorMsg) {
		if (expression == null || expression.length() < 1)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 字符串是空抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isEmpty(String expression, int errorCode, String errorMsg) {
		if (expression != null && expression.length() < 1)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 字符串不是null并且不是空抛出异常
	 * 
	 * @param expression
	 * @param message
	 */
	public static void isNotNullAndNotEmpty(String expression, int errorCode, String errorMsg) {
		if (expression != null && expression.length() > 0)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 字符串相等抛出异常
	 * 
	 * @param string1
	 * @param string2
	 * @param message
	 */
	public static void isStringEqualString(String string1, String string2, int errorCode, String errorMsg) {
		if (string1 == null || string2 == null || string1.equals(string2))
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 字符串不等抛出异常
	 * 
	 * @param string1
	 * @param string2
	 * @param message
	 */
	public static void notStringEqualString(String string1, String string2, int errorCode, String errorMsg) {
		if (string1 == null || string2 == null || !string1.equals(string2))
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 两个数字是否相等，相等抛出异常
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isIntEqualInt(int integer, int integer2, int errorCode, String errorMsg) {
		if (integer == integer2)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 不在两个数字之间，抛出异常
	 * 
	 * @param integer
	 * @param min
	 * @param max
	 * @param errorCode
	 */
	public static void isIntNotRangeInt(int integer, int min, int max, int errorCode, String errorMsg) {
		if (integer < min || integer > max)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 第一个数大于第二个数抛出异常
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isIntGTInt(int integer, int integer2, int errorCode, String errorMsg) {
		if (integer > integer2)
			throw new RequestErrorBackException(errorCode, errorMsg);
	}

	/**
	 * 一个集合任意一个错误返回相应的code
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isAllNull(Object[] objs, String errorMsg, int... errors) {
		for (int i = 0; i < objs.length; i++) {
			if (objs[i] == null)
				throw new RequestErrorBackException(errors[i], errorMsg);
		}
	}

	/**
	 * 判断手机号码是否为空或者错误
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isNullOrPhoneError(String phone, int errorCode, String errorMsg) {
		isNullAndEmpty(phone, errorCode, errorMsg);
		isFalse(Util.isPhone(phone), errorCode, errorMsg);
	}

	/**
	 * 判断邮箱是否为空或者错误
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isNullOrEmailError(String email, int errorCode, String errorMsg) {
		isNullAndEmpty(email, errorCode, errorMsg);
		isFalse(Util.isEmail(email), errorCode, errorMsg);
	}

	/**
	 * 判断是否是null或者小于等于0
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isNullOrZero(Integer integer, int errorCode, String errorMsg) {
		if (integer == null || integer.intValue() < 1)
			isFalse(false, errorCode, errorMsg);
	}

	/**
	 * 判断是否是null或者小于等于0
	 * 
	 * @param integer
	 * @param integer2
	 * @param message
	 */
	public static void isNull(Json json, int errorCode, String errorMsg) {
		if (json == null || json.value() == null || json.value().length() < 1)
			isFalse(false, errorCode, errorMsg);
	}
}
